package com.sgb.kjwl.viewmodel.vehicleManagement;

import android.content.Intent;
import androidx.databinding.ObservableField;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.view.entity.DriverAuthDetailEntity;
import com.sgb.kjwl.model.entity.transport.DriverVerifyEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.vehicleManagement.AddVehicleActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddVehicleAtvModel extends BaseViewModel {

    public ObservableField<DriverVerifyEntity> filterJson = new ObservableField<DriverVerifyEntity>(new DriverVerifyEntity());
    public ObservableField<UserAddressEntity> userAddressEntity = new ObservableField<>(new UserAddressEntity());

    AddVehicleActivity activity;
    HomeRepertory repertory;

    public AddVehicleAtvModel(AddVehicleActivity addVehicleActivity, HomeRepertory repertory) {
        this.activity = addVehicleActivity;
        this.repertory = repertory;
    }


    public void getGoodsTypeData() {
        String carsTypeString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_TYPE, "");
        String carsLengthString = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.CARS_INFO_LENGTH, "");
        List<GoodsInfoTypeEntity3> carsType = new Gson().fromJson(carsTypeString, new TypeToken<List<GoodsInfoTypeEntity3>>() {
        }.getType());
        List<GoodsInfoTypeEntity3> carsLengthType = new Gson().fromJson(carsLengthString, new TypeToken<List<GoodsInfoTypeEntity3>>() {
        }.getType());
        activity.getCarsTypeData(carsType);
        activity.getCarsLengthData(carsLengthType);
    }

    //上传多图片
    public void uploadMutiFileModel(File file, int type) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" +  "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "文件上传中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (type == activity.DRIVER_LICENS) {
                        Objects.requireNonNull(filterJson.get()).setDriverLicense(response.body().getData().toString());
                    } else if (type == activity.DRIVING_LICENS) {
                        Objects.requireNonNull(filterJson.get()).setVehicleLicense(response.body().getData().toString());
                    } else if (type == activity.CAR_PHOTO) {
                        Objects.requireNonNull(filterJson.get()).setCarImage(response.body().getData().toString());
                    }
                    activity.uploadImgSuccess(response.body().getData().toString(), type);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }


    /* 提交数据 */
    public void getDriverAuthData() {
        APIManager.getInstance().getDriverAPI().getDriverAuth(filterJson.get()).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                String msg = response.body().getMsg();
                if (response.body().getState().equals("ok")) {
                    MToast.showToast(activity, msg);
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.CAR_PUBLISH_NO_DRIVER_AUTHENTICATION, Objects.requireNonNull(filterJson.get()).getCarNumber());
                    Intent intent = new Intent();
                    intent.putExtra("state", 1);
                    activity.setResult(1, intent);
                    activity.finish();
                } else {
                    MToast.showToast(activity, msg);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {

            }
        });
    }

    /* 展示数据*/
    public void getDriverAuthDetail() {
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().driverAuthDetail().enqueue(new Callback<BaseEntity<DriverAuthDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<DriverAuthDetailEntity>> call, Response<BaseEntity<DriverAuthDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null)
                    return;
                activity.showDetails(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<DriverAuthDetailEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /*获取地址*/
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
}
